<?php

require_once(t3lib_extMgm::extPath('zlib') . 'class.tx_zlib_viewhelper.php');

abstract class tx_zlib_abstract_view
{
    /**
     * @var tx_zlib_viewhelper
     */
    private $helper;

    /**
     * @var array
     */
    private $paths = array();

    /**
     * @var array
     */
    private $vars = array();

    /**
     * @var boolean
     */
    private $strictVars = true;

    /**
     * @param string $viewScript
     * @param string $extension
     * @return string
     */
    abstract public function render($viewScript, $extension = 'phtml');

    /**
     * @param string $path
     */
    public function addPath($path)
    {
        array_unshift($this->paths, rtrim($path, '/'));
    }

    /**
     * @param string|array $spec
     * @param mixed $value
     */
    public function assign($spec, $value = null)
    {
        if (is_string($spec)) {
	        $this->vars[$spec] = $value;
        } elseif (is_array($spec)) {
            foreach ($spec as $key => $val) {
                $this->vars[$key] = $val;
            }
        } else {
            throw new Exception('assign() expects a string or array, received ' . gettype($spec), $this);
        }
    }

    /**
     * @return array
     */
    public function getVars()
    {
        return $this->vars;
    }

    /**
     * @return void
     */
    public function clearVars()
    {
        $this->vars = array();
    }

    /**
     * @return tx_zlib_viewhelper
     */
    public function getHelper()
    {
    	return $this->helper;
    }

    /**
     * @return array
     */
    public function getPaths()
    {
        return $this->paths;
    }

    /**
     * @param array $paths
     * @return void
     */
    public function setPaths(array $paths)
    {
        foreach($paths AS $path) {
            $this->addPath($path);
        }
    }

    /**
     * @param tx_zlib_viewhelper $helper
     */
    public function setHelper(tx_zlib_viewhelper $helper)
    {
    	if(null == $helper->getView()) {
    		$helper->setView($this);
    	}
    	$this->helper = $helper;
    }

    /**
     * Enable or disable strict vars
     *
     * If strict variables are enabled, {@link __get()} will raise a notice
     * when a variable is not defined.
     *
     * @param  boolean $flag
     */
    public function setStrictVars($flag = true)
    {
        $this->strictVars = $flag ? true : false;
    }

    /**
     * @param string $viewScript
     * @param string $extension
     * @return string
     */
    protected function getViewScriptPath($viewScript, $extension)
    {
        foreach($this->paths AS $path) {
            $scriptPath = $path . '/' . $viewScript . '.' . $extension;
            if(is_file($scriptPath)) {
                return $scriptPath;
            }
        }
        throw new Exception("Viewscript $viewScript.phtml not found in " . implode(",", $this->paths));
    }

    /**
     * @param string $key
     * @return mixed
     */
    private function __get($key)
    {
        if (isset($this->vars[$key])) {
            return $this->vars[$key];
        }
        if ($this->strictVars) {
            trigger_error('Key "' . $key . '" does not exist', E_USER_NOTICE);
        }
        return null;
    }

    /**
     * @param string $key
     * @return boolean
     */
    private function __isset($key)
    {
        return isset($this->vars[$key]);
    }

    /**
     * @param string $key
     * @param mixed $value
     */
    private function __set($key, $value)
    {
        $this->vars[$key] = $value;
    }

    /**
     * @param string $key
     */
    private function __unset($key)
    {
        unset($this->vars[$key]);
    }

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_abstract_view.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_abstract_view.php']);
}
